import { createRouter, createWebHistory } from 'vue-router'
import { i18n } from "@/i18n/index.js"
import { VueCookies } from 'vue-cookies'
import { IsPC } from '@/utils/IsPC'
import store from '@/store'
let language = () => {
  return i18n.global.locale
}
const routes = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/views/home/index.vue'),
    meta: {
      title: "首页",
    }
  },
  {
    path: '/application',
    name: 'application',
    component: () => import('@/views/application/index.vue'),
    redirect: '/application/rules/routine',
    meta: {
      title: "大赛报名",
    },
    children: [
      {
        path: 'rules',
        name: 'rules',
        component: () => import('@/views/application/children/rules/index.vue'),
        meta: {
          title: "规则和奖项",
        },
        children: [
          {
            path: 'routine',
            component: () => import('@/views/application/children/rules/children/routine.vue'),
            meta: {
              title: "常规赛",
            },
          },
          {
            path: 'bacteria',
            component: () => import('@/views/application/children/rules/children/bacteria.vue'),
            meta: {
              title: "细菌博弈",
            },
          },
          {
            path: 'protein',
            component: () => import('@/views/application/children/rules/children/protein.vue'),
            meta: {
              title: "蛋白设计",
            },
          }
        ]
      },
      {
        path: 'registrationForm',
        name: 'registrationForm',
        component: () => import('@/views/application/children/registrationForm/index.vue'),
        meta: {
          title: "报名表",
          hasLogin: true
        },
        children: [
          {
            path: 'routine',
            component: () => import('@/views/application/children/registrationForm/children/routine.vue'),
            meta: {
              title: "常规赛报名表",
              hasLogin: true
            },
          },
          {
            path: 'bacteria',
            component: () => import('@/views/application/children/registrationForm/children/bacteria.vue'),
            meta: {
              title: "细菌博弈报名表",
              hasLogin: true
            },
          },
          {
            path: 'protein',
            component: () => import('@/views/application/children/registrationForm/children/protein.vue'),
            meta: {
              title: "蛋白设计报名表",
              hasLogin: true
            },
          }
        ]
      },
    ]
  },
  {
    path: '/userInfo',
    component: () => import('@/views/userInfo/index.vue'),
    redirect: '/userInfo/myInfo',
    meta: {
      hasLogin: true
    },
    children: [
      {
        name: 'myInfo',
        path: 'myInfo',
        component: () => import('@/views/userInfo/children/myInfo'),
        meta: {
          hasLogin: true
        }
      },
      {
        name: 'myRegistrations',
        path: 'myRegistrations',
        component: () => import('@/views/userInfo/children/myRegistrations'),
        meta: {
          hasLogin: true
        }
      }
    ]
  },
  {
    path: '/news',
    name: 'news',
    component: () => import('@/views/news/index.vue'),
    meta: {
      title: "新闻通知",
    }
  },
  {
    path: '/competitionJudges',
    name: 'competitionJudges',
    component: () => import('@/views/competitionJudges/index.vue'),
    meta: {
      title: "大赛评委",
    }
  },
  {
    path: '/pastReview',
    name: 'pastReview',
    component: () => import('@/views/pastReview/index.vue'),
    meta: {
      title: "往期回顾",
    }
  },
  {
    path: '/relatedInformation',
    name: 'relatedInformation',
    component: () => import('@/views/relatedInformation/index.vue'),
    meta: {
      title: "相关资讯",
    }
  },
  {
    path: '/committee',
    name: 'committee',
    component: () => import('@/views/committee/index.vue'),
    meta: {
      title: "组委会"
    }
  },
  {
    path: '/myLogin',
    name: 'myLogin',
    component: () => import('@/views/myLogin/index.vue'),
    meta: {
      title: "第三方登录回调页面"
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    redirect: language() == 'zh' ? "/login/passwordLogin" : "/login/emailLogin",
    children: [
      {
        path: 'passwordLogin',
        name: 'passwordLogin',
        component: () => import('@/views/login/children/passwordLogin.vue'),
        meta: {
          unLog: true
        }
      },
      {
        path: 'smsVerificationLogin',
        name: 'smsVerificationLogin',
        component: () => import('@/views/login/children/smsVerificationLogin.vue'),
        meta: {
          unLog: true
        }
      },
      {
        path: 'emailLogin',
        name: 'emailLogin',
        component: () => import('@/views/login/children/emailLogin.vue'),
        meta: {
          unLog: true
        }
      },
    ]
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/register/index.vue'),
    redirect: language() == 'zh' ? '/register/phoneRegister' : '/register/emailRegister',
    children: [
      {
        path: 'phoneRegister',
        name: 'phoneRegister',
        component: () => import('@/views/register/children/phoneRegister.vue'),
        meta: {
          unLog: true
        }
      },
      {
        path: 'emailRegister',
        name: 'emailRegister',
        component: () => import('@/views/register/children/emailRegister.vue'),
        meta: {
          unLog: true
        }
      }
    ]
  },
  {
    path: '/changePassword',
    name: 'changePassword',
    component: () => import('@/views/changePassword/index.vue'),
    redirect: language() === 'zh' ? '/changePassword/zh' : '/changePassword/en',
    children: [
      {
        path: 'zh',
        name: 'zh',
        component: () => import('@/views/changePassword/children/zh.vue'),
        meta: {
          unLog: true
        }
      },
      {
        path: 'en',
        name: 'en',
        component: () => import('@/views/changePassword/children/en.vue'),
        meta: {
          unLog: true
        }
      },
    ]
  },




  {
    path: '/limitedTime',
    name: 'limitedTime',
    component: () => import('@/views/limitedTime/application/index.vue'),
    redirect: '/limitedTime/rules/routine',
    meta: {
      title: "大赛报名",
    },
    children: [
      {
        path: 'rules',
        component: () => import('@/views/limitedTime/application/children/rules/index.vue'),
        meta: {
          title: "规则和奖项",
        },
        children: [
          {
            path: 'routine',
            component: () => import('@/views/limitedTime/application/children/rules/children/routine.vue'),
            meta: {
              title: "常规赛",
            },
          },
          {
            path: 'bacteria',
            component: () => import('@/views/limitedTime/application/children/rules/children/bacteria.vue'),
            meta: {
              title: "细菌博弈",
            },
          },
          {
            path: 'protein',
            component: () => import('@/views/limitedTime/application/children/rules/children/protein.vue'),
            meta: {
              title: "蛋白设计",
            },
          }
        ]
      },
      {
        path: 'registrationForm',
        component: () => import('@/views/limitedTime/application/children/registrationForm/index.vue'),
        meta: {
          title: "报名表",
          hasLogin: true
        },
        children: [
          {
            path: 'routine',
            component: () => import('@/views/limitedTime/application/children/registrationForm/children/routine.vue'),
            meta: {
              title: "常规赛报名表",
              hasLogin: true
            },
          },
          {
            path: 'bacteria',
            component: () => import('@/views/limitedTime/application/children/registrationForm/children/bacteria.vue'),
            meta: {
              title: "细菌博弈报名表",
              hasLogin: true
            },
          },
          {
            path: 'protein',
            component: () => import('@/views/limitedTime/application/children/registrationForm/children/protein.vue'),
            meta: {
              title: "蛋白设计报名表",
              hasLogin: true
            },
          }
        ]
      },
    ]
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


router.beforeEach((to, from, next) => {
  if (!IsPC() && to.fullPath != '/myLogin') {
    location.href = "http://m.synbiochallenges.com" + to.fullPath;
    return;
  }
  let token = store.state.token
  if (to.meta.hasLogin) {
    if (token) {
      next();
    } else {
      store.commit('setRoute', from.path)
      router.push('/login')
    }
  } else if (to.meta.unLog) {
    if (token) {
      router.push('/')
    } else {
      next();
    }
  } else {
    next();
  }
})


export default router
